package com.midea.ai.air.ui.home.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.midea.ai.air.R;
import com.midea.ai.air.common.BaseFragment;
import com.midea.ai.air.common.Global;
import com.midea.ai.air.ui.home.adapter.EditSceneSetAdapter;
import com.midea.ai.air.ui.home.data.ParaValueModel;
import com.midea.ai.air.ui.home.data.SceneModel;
import com.midea.ai.air.view.NumberPickerDialog;
import com.midea.ai.air.view.wheelview.ArrayWheelAdapter;
import com.midea.ai.air.view.wheelview.OnWheelScrollListener;
import com.midea.ai.air.view.wheelview.WheelView;

import java.util.List;

/**
 * Created by ouyangdi on 2016/4/9.
 * 编辑房间->情景设置Fragment
 */
public class EditSceneSetFragment extends BaseFragment{

    private ListView  mListDeviceParas;
    private EditSceneSetAdapter mSetAdapter;
    private List<ParaValueModel> mParaValues;
    private String mStrScene;

    private int mCurrentIndex;
    String[] mStrRange;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_edit_scene_set, container, false);
        initData();
        initViews(view);
        return view;
    }

    public void setStrScene(String strScene) {
        mStrScene = strScene;
    }

    private void initData() {
        for(SceneModel scene:Global.mScenes) {
            if(scene.getStrScene().equals(mStrScene)) {
                mParaValues = scene.getListParas();
                break;
            }
        }
    }

    private void initViews(View view) {
        mListDeviceParas = (ListView) view.findViewById(R.id.list_device_paras);

        if(mParaValues != null) {
            mSetAdapter = new EditSceneSetAdapter(getActivity(), 0, mParaValues);
            mListDeviceParas.setAdapter(mSetAdapter);
        }

        mListDeviceParas.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                ParaValueModel item = (ParaValueModel)parent.getAdapter().getItem(position);

                if(item.getPara().equals("温度")) {
                    mStrRange = Global.sTempValue;
                } else if(item.getPara().equals("湿度")) {
                    mStrRange = Global.sHumValue;
                }

                NumberPickerDialog numDlg = new NumberPickerDialog(getActivity()).builder().setCyclic(true).setAdapter(new ArrayWheelAdapter<>(mStrRange)).addScrollingListener(new OnWheelScrollListener() {
                    @Override
                    public void onScrollingStarted(WheelView wheel) {

                    }

                    @Override
                    public void onScrollingFinished(WheelView wheel) {
                        int index = wheel.getCurrentItem();
                        Log.i("ouyangdi", "提前开机 结果：" + mStrRange[index]);
                        mCurrentIndex = index;
                    }
                }).setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        mParaValues.get(position).setValue(mStrRange[mCurrentIndex]);
                        mSetAdapter.notifyDataSetChanged();
                    }
                });
                mCurrentIndex = getCurrentIndex(mStrRange, mParaValues.get(position).getValue());
                numDlg.setCurrentItem(mCurrentIndex);
                numDlg.show();
            }
        });
    }

    private int getCurrentIndex(String[] data, String str) {
        int index = 0;
        for(int i=0; i<data.length; i++) {
            if(data[i].equals(str)) {
                index =  i;
                break;
            }
        }
        return index;
    }
}
